package com.duoqin.guard;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;

public class VerifyGuardCodeHelper {
    public static String getVerificationCode(Context context) {
        return Settings.Global.getString(context.getContentResolver(), Guard.DUOQIN_GUARD_SCREEN_LOCK_PWD);
    }

    private static boolean isCodeValid(Context context) {
        String code = getVerificationCode(context);
        return !TextUtils.isEmpty(code) && code.length() == 6;
    }

    public static boolean isCodeVerified(Context context) {
        return !isCodeValid(context) ||
                (1 == Settings.Global.getInt(context.getContentResolver(),
                        Guard.DUOQIN_GUARD_UNLOCKED, 0));
    }

    public static void setCodeVerified(Context context, boolean verified) {
        Settings.Global.putInt(context.getContentResolver(), Guard.DUOQIN_GUARD_UNLOCKED,
                verified ? 1 : 0);
    }

    public static void startVerifyCodeActivity(Context context) {
        Intent intent = new Intent("com.duoqin.guard.verifyGuardCode");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
